package com.docHunt.DAO.DAOImpl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.transform.Transformers;
import org.springframework.beans.BeanUtils;

import com.docHunt.DAO.DoctorDAO;
import com.docHunt.DTO.DoctorDTO;
import com.docHunt.model.TblDoctor;

public class DoctorDAOImpl extends BaseDAOImpl implements DoctorDAO {

	@SuppressWarnings("unchecked")
	public DoctorDTO getDoctor() {

		List<DoctorDTO> doctorDTOList = null;		
		
		try {			
			// Create criteria
			DetachedCriteria docCriteria = DetachedCriteria.forClass(TblDoctor.class);			
			docCriteria.setProjection(Projections.projectionList()
					.add(Projections.property("doctorId"),"doctorId")
					.add(Projections.property("firstName"),"firstName")
					.add(Projections.property("lastName"),"lastName"));
			docCriteria.addOrder(Order.asc("doctorId"));
			
			docCriteria.setResultTransformer(Transformers.aliasToBean(DoctorDTO.class));
						
			// Fetch the list
			doctorDTOList = (List<DoctorDTO>) super.getHibernateTemplate().findByCriteria(docCriteria);
			
			//BeanUtils.copyProperties(tblDoctorList.get(0), doctorDtoList);
			
		} catch (Exception e) {
			logger.error("DoctorDAOImpl :: Exception in method getDoctor: ", e);
		}

		return doctorDTOList.get(0);
	}
	
	public boolean addDoctorInfo(DoctorDTO doctorDTO) {
		
		TblDoctor tblDoctor = new TblDoctor();
		
		try{			
			BeanUtils.copyProperties(doctorDTO, tblDoctor);
			
			super.getHibernateTemplate().save(tblDoctor);
			
		}catch (Exception e) {
			logger.error("DoctorDAOImpl :: Exception in method addUser: ", e);
			return false;
		}
		
		return true;
	}
}
